package cn.ulc.socket.admin.service.goods.impl;

import cn.hutool.core.convert.Convert;
import cn.ulc.common.constant.enums.AvailableStatus;
import cn.ulc.common.constant.enums.DeletedStatus;
import cn.ulc.common.exception.PlatformException;
import cn.ulc.common.model.param.PaginationParam;
import cn.ulc.socket.admin.common.BaseService;
import cn.ulc.socket.admin.model.dto.goods.GoodsSpecDTO;
import cn.ulc.socket.admin.model.dto.goods.GoodsSpecListDTO;
import cn.ulc.socket.admin.model.param.goods.GoodsSpecSaveParam;
import cn.ulc.socket.admin.service.goods.GoodsSpecServ;
import cn.ulc.store.dao.goods.GoodsSpecDao;
import cn.ulc.store.entity.goods.GoodsSpec;
import cn.ulc.store.entity.staff.Staff;
import lombok.AllArgsConstructor;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@AllArgsConstructor
public class GoodsSpecServImpl extends BaseService implements GoodsSpecServ {
    GoodsSpecDao specDao;

    @Override
    public GoodsSpecListDTO findListPage(String name,Long goodsId, PaginationParam pager) {
        Staff loginUser = getCurrentUser();
        Map<String,Object> condition = new HashMap<>();
        condition.put("name",name);
        condition.put("companyId",loginUser.getCompanyId());
        condition.put("goodsId",goodsId);
        PageQuery pageQuery = initPageQuery(pager);
        pageQuery.setParas(condition);
        specDao.findListPage(pageQuery);
        return new GoodsSpecListDTO(pageQuery.getList(),pageQuery.getTotalRow());
    }

    @Override
    public GoodsSpecDTO findDetail(Long id) {
        long sid = Convert.toLong(id,0l);
        GoodsSpec goodsSpec = specDao.findById(sid);
        if(goodsSpec==null) throw new PlatformException("未获取到商品规格信息");
        return new GoodsSpecDTO(goodsSpec);
    }

    @Override
    public Boolean save(GoodsSpecSaveParam param) {
        GoodsSpec goodsSpec = new GoodsSpec();
        setBaseInfo(goodsSpec);
        goodsSpec.setGoodsId(param.getGoodsId());
        goodsSpec.setStock(param.getStock());
        goodsSpec.setSort(param.getSort());
        goodsSpec.setPrice(param.getPrice());
        goodsSpec.setPurchasePrice(param.getPurchasePrice());
        goodsSpec.setMainImage(param.getMainImage());
        goodsSpec.setName(param.getName());
        goodsSpec.setStatus(AvailableStatus.AVAILABLE);
        goodsSpec.setBarCode(param.getBarCode());
        specDao.save(goodsSpec);
        return true;
    }

    @Override
    public Boolean update(Long id, GoodsSpecSaveParam param) {
        GoodsSpec goodsSpec = specDao.findOne(id);
        if(goodsSpec==null) throw new PlatformException("未获取到商品规格信息");
        setBaseInfo(goodsSpec);
        goodsSpec.setBarCode(param.getBarCode());
        goodsSpec.setName(param.getName());
        goodsSpec.setMainImage(param.getMainImage());
        goodsSpec.setPrice(param.getPrice());
        goodsSpec.setPurchasePrice(param.getPurchasePrice());
        goodsSpec.setStock(param.getStock());
        goodsSpec.setSort(param.getSort());
        specDao.save(goodsSpec);
        return true;
    }

    @Override
    public Boolean deleted(Long id) {
        GoodsSpec entity = specDao.findOne(id);
        if(entity!=null) {
            setBaseInfo(entity);
            entity.setDeleted(DeletedStatus.DELETED);
            specDao.save(entity);
        }
        return true;
    }

    @Override
    public Boolean changeStatus(Long id, AvailableStatus status) {
        GoodsSpec entity = specDao.findOne(id);
        if(entity==null) throw new PlatformException("未获取到商品规格信息");
        setBaseInfo(entity);
        entity.setStatus(status);
        specDao.save(entity);
        return true;
    }
}
